package com.klm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.klm.common.ResultInfo;
import com.klm.model.*;
import com.klm.params.DeleteLanguageContentParams;
import com.klm.params.GetFullContentParams;
import com.klm.service.TranslateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wq
 */
@Slf4j
@Controller
@RequestMapping("/admin/translate")
public class TranslateAdminController {

    @Autowired
    private TranslateService translateService;

    /**
     * 管理页面首页
     */
    @GetMapping("")
    public String index() {
        log.info("进入管理页面");
        return "admin/translate/index";
    }

    /**
     * 语言包列表页面
     */
    @GetMapping("/items")
    public String items() {
        return "admin/translate/items";
    }

    /**
     * 语言管理页面
     */
    @GetMapping("/lang")
    public String lang() {
        return "admin/translate/lang";
    }

    /**
     * 语言包编辑页面
     */
    @GetMapping("/edit/{itemKey}")
    public String edit(@PathVariable String itemKey, Model model) {
        model.addAttribute("itemKey", itemKey);
        return "admin/translate/edit";
    }

    /**
     * 获取语言包列表数据
     */
    @PostMapping("/items")
    @ResponseBody
    public ResultInfo<Page<AutoLanguageItemModel>> getItems(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String itemKey,
            @RequestParam(required = false) String fromTable,
            @RequestParam(required = false) String fromField,
            @RequestParam(required = false) String fromDataId,
            @RequestParam(required = false) String memo) {
        return ResultInfo.build(translateService.getLanguageItemPage(
                pageNum, pageSize, itemKey, fromTable, fromField, fromDataId, memo));
    }

    /**
     * 获取语言包详情
     */
    @PostMapping("/admin/fullContent")
    @ResponseBody
    public ResultInfo<ComLangModel> getContent(@RequestBody GetFullContentParams params) {
        return ResultInfo.build(translateService.getFullContentByItemKey(params.getItemKey()));
    }

    /**
     * 获取语种列表
     */
    @PostMapping("/admin/langCodeList")
    @ResponseBody
    public ResultInfo<List<ComLangCodeModel>> getLangCodes() {
        return ResultInfo.build(translateService.getLangCodeList());
    }

    /**
     * 保存语言包内容
     */
    @PostMapping("/admin/fullContent/save")
    @ResponseBody
    public ResultInfo<Boolean> saveContent(@RequestBody SaveLangContentModel model) {
        return ResultInfo.build(translateService.saveFullContentByItemKey(model));
    }

    /**
     * 删除语言包
     */
    @PostMapping("/admin/content/remove")
    @ResponseBody
    public ResultInfo<Boolean> removeContent(@RequestBody DeleteLanguageContentParams params) {
        return ResultInfo.build(translateService.logicDelLanguageContent(params.getItemKey(), params.getFromDataId(), params.getOperator()));
    }

    /**
     * 管理后台外壳入口
     */
    @GetMapping("/shell")
    public String shell() {
        return "admin/translate/shell";
    }

    /**
     * 内容区首页（供shell.html iframe加载）
     */
    @GetMapping("/index")
    public String indexPage() {
        return "admin/translate/index";
    }
}
